package cn.iocoder.yudao.module.mqtt.controller.admin.deviceauth;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.deviceauth.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.deviceauth.DeviceAuthDO;
import cn.iocoder.yudao.module.mqtt.service.deviceauth.DeviceAuthService;

@Tag(name = "管理后台 - 设备报警授权")
@RestController
@RequestMapping("/mqtt/device-auth")
@Validated
public class DeviceAuthController {

    @Resource
    private DeviceAuthService deviceAuthService;

    @PostMapping("/create")
    @Operation(summary = "创建设备报警授权")
    @PreAuthorize("@ss.hasPermission('mqtt:device-auth:create')")
    public CommonResult<Long> createDeviceAuth(@Valid @RequestBody DeviceAuthSaveReqVO createReqVO) {
        return success(deviceAuthService.createDeviceAuth(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备报警授权")
    @PreAuthorize("@ss.hasPermission('mqtt:device-auth:update')")
    public CommonResult<Boolean> updateDeviceAuth(@Valid @RequestBody DeviceAuthSaveReqVO updateReqVO) {
        deviceAuthService.updateDeviceAuth(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备报警授权")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:device-auth:delete')")
    public CommonResult<Boolean> deleteDeviceAuth(@RequestParam("id") Long id) {
        deviceAuthService.deleteDeviceAuth(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备报警授权")
                @PreAuthorize("@ss.hasPermission('mqtt:device-auth:delete')")
    public CommonResult<Boolean> deleteDeviceAuthList(@RequestParam("ids") List<Long> ids) {
        deviceAuthService.deleteDeviceAuthListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备报警授权")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:device-auth:query')")
    public CommonResult<DeviceAuthRespVO> getDeviceAuth(@RequestParam("id") Long id) {
        DeviceAuthDO deviceAuth = deviceAuthService.getDeviceAuth(id);
        return success(BeanUtils.toBean(deviceAuth, DeviceAuthRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备报警授权分页")
    @PreAuthorize("@ss.hasPermission('mqtt:device-auth:query')")
    public CommonResult<PageResult<DeviceAuthRespVO>> getDeviceAuthPage(@Valid DeviceAuthPageReqVO pageReqVO) {
        PageResult<DeviceAuthDO> pageResult = deviceAuthService.getDeviceAuthPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceAuthRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备报警授权 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:device-auth:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceAuthExcel(@Valid DeviceAuthPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceAuthDO> list = deviceAuthService.getDeviceAuthPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备报警授权.xls", "数据", DeviceAuthRespVO.class,
                        BeanUtils.toBean(list, DeviceAuthRespVO.class));
    }

}